package com.lesson;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author liliang
 * @date 2019/10/25.
 */
public class HelloNetty {

    public static void main(String[] args) throws Exception {

        // 主线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        // 从线程组
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try{
            // netty 服务器启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 设置主从线程组
            serverBootstrap.group(bossGroup, workGroup)
                    // 设置 NIO 双向通道
                    .channel(NioServerSocketChannel.class)
                    // 子处理器,用于处理 workGroup
                    .childHandler(new HelloServerInitializer());

            // 启动 Server,并且设置端口号,同时启动方式为同步
            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
            // 监听关闭的 Channel,设置为同步方式
            channelFuture.channel().closeFuture().sync();
        }finally {
            // 优雅的关闭
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }


    }
}
